#pragma once
#include"Log.hpp"
#include"Task.hpp"
#include"ThreadPool.hpp"
#include<pthread.h>
#include<time.h>
#include<signal.h>
#include<unistd.h>

ThreadPool<Task>* g_tp;

void* Product(void* arg)
{
  pthread_detach(pthread_self());
  ThreadPool<Task>* tp = static_cast<ThreadPool<Task>*>(arg);
  while(1)
  {
    const char* str = "+-*/%";
    int x = rand() % 20;
    int y = rand() % 10;
    char op = str[rand() % 5];
    //cout << "发布的任务是: " << x <<" " << op <<" " << y << " = ?" << endl;  
    string log_message = "发布的任务是:" + to_string(x) + " " + op + " " + to_string(y) + " = ?" ; 
    //LOG(NOMAL) << " " << log_message << endl; 
    LOG(NORMAL,log_message);
    Task task(x,y,op);
    sleep(1);
    tp->push(task);
  }
}

void handle(int sig)
{
   g_tp->ExitThread();
}
int main()
{
  srand((size_t)time(nullptr));
  signal(2,handle);
  // ThreadPool<Task>* tp = new ThreadPool<Task>();
  ThreadPool<Task>* tp = ThreadPool<Task>::GetInstance();
  g_tp = tp;
  int n = tp->InitThreadPool();
  cout << "线程池当中的线程个数为:" << n << endl;
  pthread_t pro;
  pthread_create(&pro,nullptr,Product,(void*)tp);
  pthread_join(pro,nullptr);
  return 0;
}
